variables:
  STAGE_DOCKER_COMPOSE_HOME: infrastructure/stage
  STAGE_SERVER_HOST: stage.angrywords.ru
  STAGE_SERVER_USER: cicd
  PROD_DOCKER_COMPOSE_HOME: infrastructure/prod
  PROD_SERVER_HOST: angrywords.ru
  PROD_SERVER_USER: cicd

services:
  - docker:20.10.18-dind

stages:
  - build_stage
  - deploy_stage
  - build_prod
  - deploy_prod

build_stage:
  stage: build_stage
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "DEPLOYMENT_ID=$CI_COMMIT_SHORT_SHA" >> $STAGE_DOCKER_COMPOSE_HOME/.env
    - echo "CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE" >> $STAGE_DOCKER_COMPOSE_HOME/.env
  script:
    - docker compose -f "$STAGE_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "stage_website" build
    - docker compose -f "$STAGE_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "stage_website" push
  only:
    - main

deploy_stage:
  stage: deploy_stage
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - apt-get update && apt-get install -y openssh-client
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "DEPLOYMENT_ID=$CI_COMMIT_SHORT_SHA" >> $STAGE_DOCKER_COMPOSE_HOME/.env
    - echo "CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE" >> $STAGE_DOCKER_COMPOSE_HOME/.env
  script:
    - mkdir ~/.ssh/
    - ssh-keyscan $STAGE_SERVER_HOST >> ~/.ssh/known_hosts
    - cp $STAGE_SSH_KEY ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - DOCKER_HOST=ssh://$STAGE_SERVER_USER@$STAGE_SERVER_HOST
    - docker compose -f "$STAGE_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "stage_website" down --remove-orphans
    - docker compose -f "$STAGE_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "stage_website" pull
    - docker compose -f "$STAGE_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "stage_website" up -d
    - sleep 5s
    - ssh $STAGE_SERVER_USER@$STAGE_SERVER_HOST 'docker exec nginx nginx -s reload'
  only:
    - main

build_prod:
  stage: build_prod
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "DEPLOYMENT_ID=$CI_COMMIT_SHORT_SHA" >> $PROD_DOCKER_COMPOSE_HOME/.env
    - echo "CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE" >> $PROD_DOCKER_COMPOSE_HOME/.env
  script:
    - docker compose -f "$PROD_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "website" build
    - docker compose -f "$PROD_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "website" push
  only:
    - main
  when: manual
  allow_failure: false

deploy_prod:
  stage: deploy_prod
  image: tiangolo/docker-with-compose:2023-05-01
  before_script:
    - apt-get update && apt-get install -y openssh-client
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY_IMAGE
    - echo "DEPLOYMENT_ID=$CI_COMMIT_SHORT_SHA" >> $PROD_DOCKER_COMPOSE_HOME/.env
    - echo "CI_REGISTRY_IMAGE=$CI_REGISTRY_IMAGE" >> $PROD_DOCKER_COMPOSE_HOME/.env
  script:
    - mkdir ~/.ssh/
    - ssh-keyscan $PROD_SERVER_HOST >> ~/.ssh/known_hosts
    - cp $PROD_SSH_KEY ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - DOCKER_HOST=ssh://$PROD_SERVER_USER@$PROD_SERVER_HOST
    - docker compose -f "$PROD_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "website" down --remove-orphans
    - docker compose -f "$PROD_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "website" pull
    - docker compose -f "$PROD_DOCKER_COMPOSE_HOME/docker-compose.yml" -p "website" up -d
    - sleep 5s
    - ssh $PROD_SERVER_USER@$PROD_SERVER_HOST 'docker exec nginx nginx -s reload'
  only:
    - main